<template>
  <el-dialog
    :title="!dataForm.usmId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="usmName">
      <el-input v-model="dataForm.usmName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="usmPassword">
      <el-input v-model="dataForm.usmPassword" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="usmNickname">
      <el-input v-model="dataForm.usmNickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="usmGender">
      <el-input v-model="dataForm.usmGender" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="手机号码" prop="usmMobile">
      <el-input v-model="dataForm.usmMobile" placeholder="手机号码"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="usmEmail">
      <el-input v-model="dataForm.usmEmail" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="usmHeader">
      <el-input v-model="dataForm.usmHeader" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="usmBirth">
      <el-input v-model="dataForm.usmBirth" placeholder="生日"></el-input>
    </el-form-item>
    <el-form-item label="所在城市" prop="usmCity">
      <el-input v-model="dataForm.usmCity" placeholder="所在城市"></el-input>
    </el-form-item>
    <el-form-item label="职业" prop="usmJob">
      <el-input v-model="dataForm.usmJob" placeholder="职业"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="usmCreateTime">
      <el-input v-model="dataForm.usmCreateTime" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="用户来源" prop="usmSourceType">
      <el-input v-model="dataForm.usmSourceType" placeholder="用户来源"></el-input>
    </el-form-item>
    <el-form-item label="社交账号ID" prop="usmSocialUid">
      <el-input v-model="dataForm.usmSocialUid" placeholder="社交账号ID"></el-input>
    </el-form-item>
    <el-form-item label="社交账号token" prop="usmAccessToken">
      <el-input v-model="dataForm.usmAccessToken" placeholder="社交账号token"></el-input>
    </el-form-item>
    <el-form-item label="社交账号token过期时间" prop="usmExpiresIn">
      <el-input v-model="dataForm.usmExpiresIn" placeholder="社交账号token过期时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          usmId: 0,
          usmName: '',
          usmPassword: '',
          usmNickname: '',
          usmGender: '',
          usmMobile: '',
          usmEmail: '',
          usmHeader: '',
          usmBirth: '',
          usmCity: '',
          usmJob: '',
          usmCreateTime: '',
          usmSourceType: '',
          usmSocialUid: '',
          usmAccessToken: '',
          usmExpiresIn: ''
        },
        dataRule: {
          usmName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          usmPassword: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          usmNickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          usmGender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          usmMobile: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' }
          ],
          usmEmail: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          usmHeader: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          usmBirth: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          usmCity: [
            { required: true, message: '所在城市不能为空', trigger: 'blur' }
          ],
          usmJob: [
            { required: true, message: '职业不能为空', trigger: 'blur' }
          ],
          usmCreateTime: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          usmSourceType: [
            { required: true, message: '用户来源不能为空', trigger: 'blur' }
          ],
          usmSocialUid: [
            { required: true, message: '社交账号ID不能为空', trigger: 'blur' }
          ],
          usmAccessToken: [
            { required: true, message: '社交账号token不能为空', trigger: 'blur' }
          ],
          usmExpiresIn: [
            { required: true, message: '社交账号token过期时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.usmId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.usmId) {
            this.$http({
              url: this.$http.adornUrl(`/authserver/userrecord/info/${this.dataForm.usmId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.usmName = data.userRecord.usmName
                this.dataForm.usmPassword = data.userRecord.usmPassword
                this.dataForm.usmNickname = data.userRecord.usmNickname
                this.dataForm.usmGender = data.userRecord.usmGender
                this.dataForm.usmMobile = data.userRecord.usmMobile
                this.dataForm.usmEmail = data.userRecord.usmEmail
                this.dataForm.usmHeader = data.userRecord.usmHeader
                this.dataForm.usmBirth = data.userRecord.usmBirth
                this.dataForm.usmCity = data.userRecord.usmCity
                this.dataForm.usmJob = data.userRecord.usmJob
                this.dataForm.usmCreateTime = data.userRecord.usmCreateTime
                this.dataForm.usmSourceType = data.userRecord.usmSourceType
                this.dataForm.usmSocialUid = data.userRecord.usmSocialUid
                this.dataForm.usmAccessToken = data.userRecord.usmAccessToken
                this.dataForm.usmExpiresIn = data.userRecord.usmExpiresIn
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/authserver/userrecord/${!this.dataForm.usmId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'usmId': this.dataForm.usmId || undefined,
                'usmName': this.dataForm.usmName,
                'usmPassword': this.dataForm.usmPassword,
                'usmNickname': this.dataForm.usmNickname,
                'usmGender': this.dataForm.usmGender,
                'usmMobile': this.dataForm.usmMobile,
                'usmEmail': this.dataForm.usmEmail,
                'usmHeader': this.dataForm.usmHeader,
                'usmBirth': this.dataForm.usmBirth,
                'usmCity': this.dataForm.usmCity,
                'usmJob': this.dataForm.usmJob,
                'usmCreateTime': this.dataForm.usmCreateTime,
                'usmSourceType': this.dataForm.usmSourceType,
                'usmSocialUid': this.dataForm.usmSocialUid,
                'usmAccessToken': this.dataForm.usmAccessToken,
                'usmExpiresIn': this.dataForm.usmExpiresIn
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
